package office.service.hr.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import office.action.hr.form.RecruitPlanForm;
import office.bo.hr.IRecruitPlan;
import office.bo.hr.impl.CheckFolw;
import office.bo.hr.impl.RecruitPlan;
import office.bo.person.LeaveRequest;
import office.domainset.BizDomainProviderSetImpl;
import office.service.finance.impl.CheckFolwServiceImpl;
import office.service.hr.IRecruitPlanService;
import office.service.system.ISystemService;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public class RecruitPlanServiceImpl extends CheckFolwServiceImpl implements IRecruitPlanService{
	@Resource
	private ISystemService systemService;
	public ISystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}
	
	/**
	 * 获取招聘计划信息
	 * @return
	 * @throws BusinessException
	 */
	public List getRecruitPlanList(String userId,String roleName,String deptId,String companyId) throws BusinessException{
		List listData = new ArrayList() ;
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Map map = new HashMap();
			map.put("userId", userId);
			map.put("checkRole", roleName);
			map.put("deptId", systemService.getDepartment(deptId,companyId));
			listData = domainProviderSetImpl.getDao().findByHqlNameAndDomian(domainProviderSetImpl.getRecruitPlanDP(), "findUserId", map);
			//getFindUserId
			//List list = domainProviderSetImpl.getDao().findAllByDomian(domainProviderSetImpl.getRecruitPlanDP());
//			
//			Map mapPipeline = this.getPipelineMap();
//			RecruitPlanWrapper wrapper;
//			for(int i=0;i<list.size();i++){
//				RecruitPlan bo = (RecruitPlan)list.get(i);
//				wrapper = new RecruitPlanWrapper();
//				wrapper.copyToWrapper(bo,mapPipeline);
//				listData.add(wrapper);
//			}
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取招聘信息失败!", e);
		}
		return listData;
	}
	/**
	 * 编辑招聘计划
	 * @throws BusinessException
	 */
	public void editRecruitPlan(RecruitPlanForm objctForm)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			RecruitPlan bo = null;
			if(null==objctForm.getId() || ("").equals(objctForm.getId())){
				bo = (RecruitPlan)domainProviderSetImpl.getRecruitPlanDP().newEmptyObj();
			}else{
				bo = (RecruitPlan)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getRecruitPlanDP(), objctForm.getId());
			}
			bo =(RecruitPlan) objctForm.copyPropertiesToBO(bo);
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
				bo.setUserId(objctForm.getUserId());
				bo.setState("0");
				bo.setEntryRole(objctForm.getEntryRole());
				Calendar c = Calendar.getInstance();
				SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd");
				bo.setEntryDate(dateFmt.format(c.getTime()));
				bo.setEntryRole(objctForm.getEntryRole());
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
			
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑招聘计划失败!", e);
		}
	}
	
	public Map getPipelineMap()throws BusinessException{
		return Util.parsePropertiesReturnMap("office","pipeline.size");
	}
	/**
	 * 根据id获取对象
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	public RecruitPlanForm getById(String id)throws BusinessException{
		RecruitPlanForm form = new RecruitPlanForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			IRecruitPlan bo = (IRecruitPlan)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getRecruitPlanDP(), id);
			Map mapPipeline = this.getPipelineMap();
			form.copyPropertiesFromBO(bo,mapPipeline);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取招聘计划对象失败!", e);
		}
		return form;
	}
	/**
	 * 根据id删除对象
	 * @param id
	 * @throws BusinessException
	 */
	public void deleteById(String id)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			domainProviderSetImpl.getDao().deleteById(domainProviderSetImpl.getRecruitPlanDP(), id);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取招聘计划对象失败!", e);
		}
	}
	
	/**
	 * 修改状态
	 * @param objctForm
	 * @throws Exception
	 */
	public void checkDate(RecruitPlanForm objctForm)throws Exception{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			RecruitPlan bo = (RecruitPlan)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getRecruitPlanDP(), objctForm.getId());
			bo.setState(objctForm.getState());
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取招聘计划对象失败!", e);
		}
	}
	@Override
	public CheckFolw getCheckFolwById(String id) throws BusinessException {
		RecruitPlan recruitPlan = null;
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			recruitPlan = (RecruitPlan)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getRecruitPlanDP(), id);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取审批对象信息失败!", e);
		}
		return recruitPlan;
	}

}
